<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <el-form-item label="资源标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入资源标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="visible">
        <el-select v-model="queryParams.visible" placeholder="请选择是否显示" clearable>
          <el-option
            v-for="dict in dict.type.common_visible"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="格式" prop="format">
        <el-input
          v-model="queryParams.format"
          placeholder="请输入格式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item v-hasRole="['admin']" label="上传用户" prop="uploadUser">
        <el-input
          v-model="queryParams.uploadUser"
          placeholder="请输入上传用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="方向" prop="directionId">
        <el-select v-model="queryParams.directionId" placeholder="请选择方向" clearable>
          <el-option
            v-for="dict in directionList"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否热门" prop="isHot">
        <el-select v-model="queryParams.isHot" placeholder="请选择是否热门" clearable>
          <el-option
            v-for="dict in dict.type.bs_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否显示" prop="visible">
        <el-select v-model="queryParams.visible" placeholder="请选择是否显示" clearable>
          <el-option
            v-for="dict in dict.type.common_visible"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['blogs:resource:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['blogs:resource:edit']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['blogs:resource:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['blogs:resource:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="resourceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="资源标题" align="center" prop="title">
        <template slot-scope="scope">
          <el-link type="primary" :href="baseApi + scope.row.url">{{ scope.row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="资源大小" align="center" prop="contentLength" />
      <el-table-column label="状态" align="center" prop="visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.common_visible" :value="scope.row.visible" />
        </template>
      </el-table-column>
      <el-table-column label="是否热门" align="center" prop="isHot">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bs_yes_no" :value="scope.row.isHot" />
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="格式" align="center" prop="format" />
      <el-table-column v-hasRole="['admin']" label="上传用户" align="center" prop="uploadUser" />
      <el-table-column label="方向" align="center" prop="directionId">
        <template slot-scope="scope">
          <dict-tag :options="directionList" :value="scope.row.directionId ? [scope.row.directionId] : null" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['blogs:resource:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['blogs:resource:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资源对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="资源标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入资源标题" />
        </el-form-item>
        <el-form-item label="资源描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="资源url" prop="url">
          <file-upload v-model="form.url" :limit="1" @getFileSize="getFileSize" @getExt="getExt" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.visible">
            <el-radio
              v-for="dict in dict.type.common_visible"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="方向" prop="directionId">
          <el-select v-model="form.directionId" placeholder="请选择方向">
            <el-option
              v-for="dict in directionList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-hasRole="['admin']" label="是否热门">
          <el-radio-group v-model="form.isHot">
            <el-radio
              v-for="dict in dict.type.bs_yes_no"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listResource, getResource, delResource, addResource, updateResource } from '@/api/blogs/resource'
import { allListDirection } from '@/api/blogs/direction'
export default {
  name: 'Resource',
  dicts: ['common_visible', 'bs_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资源表格数据
      resourceList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        visible: null,
        format: null,
        uploadUser: null,
        directionId: null,
        isHot: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: '资源标题不能为空', trigger: 'blur' }
        ],
        url: [
          { required: true, message: '资源url不能为空', trigger: 'blur' }
        ],
        directionId: [
          { required: true, message: '方向不能为空', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '排序不能为空', trigger: 'blur' }
        ],
        format: [
          { required: true, message: '格式不能为空', trigger: 'blur' }
        ]
      },
      // 方向表格数据
      directionList: [],
      baseApi: process.env.VUE_APP_BASE_API
    }
  },
  created() {
    this.getAllDireactionList()
    this.getList()
  },
  methods: {
    /** 查询资源列表 */
    getList() {
      this.loading = true
      listResource(this.queryParams).then(response => {
        this.resourceList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 查询全部方向列表 */
    getAllDireactionList() {
      allListDirection().then(response => {
        this.directionList = response.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        description: null,
        contentLength: null,
        url: null,
        visible: 0,
        sort: null,
        format: null,
        uploadUser: null,
        createTime: null,
        updateTime: null,
        userId: null,
        directionId: null,
        isHot: 0
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加资源'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getResource(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改资源'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateResource(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addResource(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除资源编号为"' + ids + '"的数据项？').then(function() {
        return delResource(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('blogs/resource/export', {
        ...this.queryParams
      }, `resource_${new Date().getTime()}.xlsx`)
    },
    getFileSize(size) {
      this.form.contentLength = size
    },
    getExt(ext) {
      this.form.format = ext
    }
  }
}
</script>
